# -*- coding: utf-8 -*- 
from django.http import Http404 # wyjątek do podnoszenia jak mam zły dzi
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.http import HttpResponse #potrzebny żeby ręcznie zrobić responsa do popupów
from django.utils.encoding import force_unicode #potrzebny żeby javascript nie krzyczał
from intraorg.members.models import *
from intraorg.projects.models import *
from django.newforms import form_for_model
from django.contrib.auth.decorators import login_required
from django import newforms as forms
from django.contrib.auth.models import User
from django.db import transaction

@login_required
def profile(request,id):
  member = get_object_or_404(Member,id=id)
  groups = GroupMember.objects.all().filter(member = member)
  projects = ProjectTeam.objects.all().filter(member = member)
  return render_to_response('members/profile.html',{'member':member,'groups':groups,'projects':projects})

@login_required
def members(request,**kwargs):
  members = Member.objects.all().filter(verification=1).order_by('membership','user','nickname')
  return render_to_response('members/list.html',{'members':members})

@login_required
def groups(request,**kwargs):
  groups = Group.objects.all().order_by('name')
  return render_to_response('members/group_list.html',{'groups':groups})

@login_required
def group(request,id):
  group = get_object_or_404(Group,id=id)
  members = GroupMember.objects.all().filter(group = group)
  return render_to_response('members/group.html',{'members':members,'group':group})


class RecruitForm(forms.Form):
    first_name = forms.CharField(label=u'Imię', max_length=20, \
        required=True, help_text=u'Imię, max 20 znaków.') 
    last_name = forms.CharField(label=u'Nazwisko', max_length=30, \
        required=True, help_text=u'Nazwisko, max 30 znaków.')
    birthday  =  forms.fields.DateField(label=u'Data urodzenia',\
        required=True, help_text=u'Data urodzenia.')
    nickname = forms.CharField(label=u'nick', max_length=20, \
        required=True, help_text=u'twój nick, Jak chcesz żebyśmy cię nazywali?, max 20 znaków.') 
    login = forms.CharField(label=u'login', max_length=20, \
        required=True, help_text=u'wybierz unikalny login do systemu , max 20 znaków.') 
    interests = forms.CharField(label=u'zainteresowania',  \
        required=True, help_text=u'opisz obszary swoich zainteresowań, związanych z branżą IT i poza nią') 
    cv = forms.CharField(label=u'cv',  \
        required=False, help_text=u'Prześlij nam swoje CV.') 
    gg = forms.CharField(label=u'gg', max_length=20, \
        required=False, help_text=u'twój numer gg') 
    jabber = forms.EmailField(label=u'jabber', max_length=40, \
        required=False, help_text=u'twój adres jabbera.') 
    email = forms.EmailField(label=u'email', max_length=40, \
        required=True, help_text=u'twój adres email.') 
    skype = forms.CharField(label=u'skype', max_length=20, \
        required=False, help_text=u'twój skype id') 
    avatar = forms.CharField(label=u'avatar',\
        required=False, help_text=u'avatar który będzie widoczny na tej stronie')
    foto = forms.CharField(label=u'fotka',\
        required=False, help_text=u'twoja podobizna, żebyśmy wiedzieli jak wyglądasz')
    
        

#@login_required
@transaction.commit_on_success
def recruit(request):
    """Formularz rekrutacyjny"""
    comment=""
    projects = Project.objects.all().filter(end_date__isnull =True).order_by("-start_date")
    Form = RecruitForm#,fields=('name','surname','about','interests','cv','avatar','login','nickname','birthday','email','jabber','gg','skype'))
    if request.method == 'POST':
      form = Form(request.POST)
      if form.is_valid():
        user=User(\
             username=form.cleaned_data["login"],\
             first_name=form.cleaned_data["first_name"],\
             last_name=form.cleaned_data["last_name"],\
             email=form.cleaned_data["email"],\
             is_staff=False, \
             is_active=False, \
             is_superuser=False\
             )
        user.save()     
        Member(\
        user= user,\
        nickname = form.cleaned_data["nickname"],\
        interests = form.cleaned_data["interests"],\
        cv = form.cleaned_data["cv"],\
        gg = form.cleaned_data["gg"],\
        jabber = form.cleaned_data["jabber"],\
        skype = form.cleaned_data["skype"],\
        avatar = form.cleaned_data["avatar"],\
        foto = form.cleaned_data["foto"],\
        birthday = form.cleaned_data["birthday"],\
        membership = MembershipType.objects.get(name=u"Kandydat") ,\
        verification = 0 ,\
        ).save()
        comment=u"Dziękujemy za wypełnienie zgłoszenia, wkrótce otrzymasz od nas informacje dotyczące rekrutacji."
        #return HttpResponseRedirect('/zlecenia/weryfikacja_do_korespondencji/')
    else:
      form = Form()
    return render_to_response('members/recruit.html', {'form':form,'projects':projects,'comment':comment})
